package hc;

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.topic.ITopic;
import test.PerformanceAnalyze;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TopicTest02 {

    test.PerformanceAnalyze analyze = new PerformanceAnalyze();


    void start() {
        Config config = new Config();
        config.getNetworkConfig().setPublicAddress("127.0.0.1");
        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config);


        int size = 200;
        ExecutorService executorService = Executors.newFixedThreadPool(size);

        for (int i = 0; i < size; i++) {
            int finalI = i;
            executorService.submit(() -> {
                ITopic<Long> topic = hzInstance.getTopic("topic"+ finalI);

                while (true) {
                    analyze.meter();
                    long start= System.currentTimeMillis();
                    topic.publish(start);
                    analyze.delay(System.currentTimeMillis() - start);
                }
            });
        }

    }

    public static void main(String[] args) {

        new TopicTest02().start();
    }
}
